<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Common\Model;

use Think\Model;

/**
 * 图片 Model模型类
 */
class PhotoModel extends Model{
	
	/**
	 * @var string
	 */
	public $type;
	
	public $status = 1;
	
	/**
	 * 生成查询条件
	 * 
	 * @param array $where
	 * @return array
	 */
	public function createWhere($where) {
		if ($this->type) {
			$where ['type'] = $this->type;
		}
		
		$where ['status'] = $this->status;
		
		return $where;
	}
	
	/**
	 * 清除实例对象图片缓存
	 *
	 * @param number $target_id	服务项目、美容师编号、美容院编号
	 */
	public function clearCache($target_id) {
		$key = $this->getCacheKey($target_id);
		S($key, null);
	}
	
	/**
	 * 编辑实例的图片
	 *
	 * @param number $target_id		服务项目、美容师编号、美容院编号
	 * @param array $submitPhotos	图片
	 * @param string $number		上传编号
	 * @return boolean
	 */
	public function editTargetPhotos($target_id, $submitPhotos = array(), $number) {
		$photos = $this->getTargetPhotos($target_id);
		$photoNames = array();
		if ($photos) {
			$photoNames = array_map(function ($var) {
				return $var ['img_name'];
			}, $photos);
		}
	
		$deletePhoto = array_diff($photoNames, $submitPhotos);
		$addPhoto = array_diff($submitPhotos, $photoNames);
		if ($deletePhoto) {
			foreach ($deletePhoto as $dKey => $dValue) {
				$index = array_search($dValue, $photoNames);
				if ($index !== false) {
					$deleteResult = $this->where(array('id' => $photos [$index] ['id']))->save(array('status' => 0));
					if (!$deleteResult) {
						return false;
					}
				}
			}
			//触发文件清除方法
		}
	
		if ($addPhoto) {
			$sort = $this->getSortByTarget($target_id);
			foreach ($addPhoto as $aKey => $aValue) {
				++$sort;
				$photoData = array(
					'target_id' => $target_id,
					'img_name' => $aValue,
					'type' => $this->type,
					'sort' => $sort,
					'upload_number' => $number,
					'created' => time()
				);
				$addResult = $this->add($photoData);
				if (!$addResult) {
					return false;
				}
			}
		}
	
		$this->clearCache($target_id);
		return true;
	}
	
	/**
	 * 获取某个实例图片最大排序
	 * 
	 * @param number $target_id	服务项目、美容师编号、美容院编号
	 * @return Ambigous <number, unknown>
	 */
	public function getSortByTarget($target_id) {
		$where = $this->createWhere(array(
			'target_id'	=> $target_id
		));
		
		$result = $this->field(array('sort'))
					->where($where)
					->order('sort DESC')
					->limit(1)
					->find();
		return $result?$result['sort']:1;
	}
	
	/**
	 * 生成缓存key
	 * 
	 * @param number $target_id	服务项目、美容师编号、美容院编号
	 * @return string
	 */
	public function getCacheKey($target_id) {
		return 'Photo_'.($this->type?'_'.$this->type.'_':'').$target_id;
	}
	
	/**
	 * 查询首页轮播图
	 */
	public function findHomeCarousel() {
		$cacheKey = 'Home_Carousel';
		$list = S($cacheKey);
		if (!$list) {
			$list = $this->where(array('status' => 1, 'type'=> 'h_carousel'))->order('sort')->select();
		}
		
		return $list;
	}
	
	/**
	 * 清除首页滚动缓存
	 */
	public function clearHomeCarouselCache() {
		S('Home_Carousel', null);
	}
	
	/**
	 * 获取实例所有图片
	 * 
	 * @param number $target_id	服务项目、美容师编号、美容院编号
	 * @return boolean|array
	 */
	public function getTargetPhotos($target_id) {
		$key = $this->getCacheKey($target_id);
		
		$photos = S($key);
		if (!$photos) {
			$where = $this->createWhere(array(
				'target_id'	=> $target_id,
			));
			$photos = $this->where($where)->select();
			S($key, $photos);
		}
		
		return $photos;
	}
	
}